package db;

import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Properties;

/**
 * @Auther: 程嘉明
 * @Date: 2018/8/21/021 15:32
 * @Type：工具类
 * @Description: 数据库链接类
 * 将数据库链接放置在本类中的静态方法getConnection
 * 数据库中的getConnection方法将提供Statement对象
 */
public class Jdbc {
    public static Connection conn;
    public static Statement stat;
    public static Statement stat1;
    public static Statement stat2;
    /**
     * 构造器私有化防止被初始化
     */
    private Jdbc(){

    }
    /**
     * @throws Exception
     * @Description:静态方法，链接数据库
     */
    public static void getConnection() throws Exception {
        //加载属性文件，项目中避免将登录项目放在程序中，放在属性文件中便于修改
        Properties prop = new Properties();
        prop.load(new FileReader("./src/db/jdbc.properties"));
        //获取数据库的各项值
        String drive = prop.getProperty("drive");
        String pass = prop.getProperty("pass");
        String user = prop.getProperty("user");
        String url = prop.getProperty("url");
        //加载驱动类
        Class.forName(drive);
        //初始化Connection和Statement对象
        conn = DriverManager.getConnection(url, user, pass);
        stat = conn.createStatement();
        stat1=conn.createStatement();
        stat2=conn.createStatement();
    }
}